package com.aaa.stream.demo01;

import org.junit.Test;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;


/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/11/5
 */
public class Test01 {
    //提取和组合
    //流也可以进行合并、去重、限制、跳过等操作
    public static void main(String[] args) {
        String[] arr1={"a","b","c","d"};
        String[] arr2={"d","e","f","g"};
        Stream<String> stream1=Stream.of(arr1);
        Stream<String> stream2=Stream.of(arr1);

        //合并流
        Stream.concat(stream1,stream2).forEach(System.out::println);

        //去重--distinct
        Stream.concat(stream1,stream2).distinct().forEach(System.out::println);

        //获取前n个元素--limit
        Stream.concat(stream1,stream2).limit(5).forEach(System.out::println);

        //跳过前n个元素
        Stream.concat(stream1,stream2).skip(2).forEach(System.out::println);

        /*find操作--findFirst和findAny*/

    }
    @Test
    public void test02(){
        Integer[] arr={3,24,1,7,5};
        Optional<Integer> reduce= Arrays.stream(arr).reduce((t1,t2)->t1+=t2);
        System.out.println("reduce = " + reduce.get());
    }
}
